home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 August (Alt) / CHIP 2005-08.1.iso / program / torrent / tracker.exe / MyHandler.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-05  |  2.9 KB  |  120 lines

  1. /**
  2.  **    File ......... MyHandler.h
  3.  **    Published ....  2004-04-20
  4. **/
  5. /*
  6. Copyright (C) 2004  grymse@alhem.net
  7.  
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21. */
  22. #ifndef _MYHANDLER_H
  23. #define _MYHANDLER_H
  24.  
  25. #include <map>
  26. #include <string>
  27. #include "MinderHandler.h"
  28. #include <libxml/tree.h>
  29. #include <vector>
  30. #include <stdint.h>
  31. #include <Utility.h>
  32. #include "Configuration.h"
  33. #include "key.h"
  34. #include "MyMinionSocket.h"
  35. #include "TrackerSock.h"
  36.  
  37.  
  38. class MyHandler : public MinderHandler
  39. {
  40. public:
  41.     struct PEER {
  42.         PEER(const std::string& id,const std::string& address,port_t p)
  43.         :escaped_peer_id(id),ip(address),port(p)
  44.         ,uploaded(0),downloaded(0),left(0)
  45.         ,updated(true),completed(false),stopped(false)
  46.         ,last_contact(time(NULL))
  47.         {
  48.             for (size_t i = 0; i < id.size(); i += 3)
  49.             {
  50.                 int x;
  51.                 sscanf( (char *)id.substr(i + 1,2).c_str(),"%02X",&x);
  52.                 peer_id[i / 3] = x;
  53.             }
  54.         }
  55.         void update(const std::string& u,const std::string& d,const std::string& l) {
  56.             uploaded = Utility::atoi64(u);
  57.             downloaded = Utility::atoi64(d);
  58.             left = Utility::atoi64(l);
  59.             updated = true;
  60.         }
  61.         void stop() {
  62.             if (!stopped)
  63.                 updated = true;
  64.             stopped = true;
  65.         }
  66.         void complete() {
  67.             if (!completed)
  68.                 updated = true;
  69.             completed = true;
  70.         }
  71.         std::string escaped_peer_id;
  72.         unsigned char peer_id[20];
  73.         std::string ip;
  74.         port_t port;
  75.         uint64_t uploaded;
  76.         uint64_t downloaded;
  77.         uint64_t left;
  78.         bool updated;
  79.         bool completed;
  80.         bool stopped;
  81.         time_t last_contact;
  82.     };
  83.     typedef std::vector<PEER *> peer_v;
  84.     typedef std::map<std::string,peer_v> peer_m;
  85. public:
  86.     MyHandler(const std::string& );
  87.     ~MyHandler();
  88.  
  89.     std::string GetVersion() { 
  90. //        std::string str = _VERSION_; return str; 
  91.         return GetString("server/identity");
  92.     }
  93.     unsigned char *GetKey_m2minion() { return k1; }
  94.  
  95.     int GetInt(const std::string& );
  96.     std::string GetString(const std::string& );
  97.     bool GetBoolean(const std::string& );
  98.  
  99.     std::string escape(const std::string& );
  100.  
  101.     PEER *reg_peer(const std::string& hash,
  102.         const std::string& peer_id,
  103.         const std::string& ip,
  104.         port_t port);
  105.  
  106.     void CreatePeers(TcpSocket *,const std::string& hash,size_t);
  107.     void SendList();
  108.     void CheckDropped();
  109.     void SendInit(MyMinionSocket *);
  110.     size_t MapSize();
  111.     void HtmlReport(TrackerSock *);
  112.  
  113. private:
  114.     Configuration m_config;
  115.     peer_m m_peers;
  116. };
  117.  
  118.  
  119. #endif // _MYHANDLER_H
  120.